package com.javasee.rain.stock.service;

import com.javasee.rain.stock.mapper.QingxuMapper;
import com.javasee.rain.stock.model.Qingxu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 情绪数据服务类
 * 
 * @author : xinyanfei
 * @date : 2025/10/10
 * Copyright (c) 2025, 京东商城  All Rights Reserved.
 */
@RestController
@RequestMapping("/api/qingxu")
@CrossOrigin(origins = "*") // 允许跨域访问
public class QingxuService {

    @Autowired
    private QingxuMapper qingxuMapper;

    /**
     * 根据日期获取情绪数据
     */
    @GetMapping("/{dateTime}")
    public ResponseEntity<List<Qingxu>> getQingxuByDate(@PathVariable String dateTime) {
        try {
            System.out.println("查询情绪数据，日期: " + dateTime);
            List<Qingxu> qingxuList = qingxuMapper.selectByDate(dateTime);
            return ResponseEntity.ok(qingxuList);
        } catch (Exception e) {
            System.err.println("查询情绪数据失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 获取所有情绪数据（默认返回固定日期的数据）
     */
    @GetMapping("/get")
    public ResponseEntity<Map<String, Object>> getAllQingxu() {
        try {
           List<Qingxu> qingxuList = qingxuMapper.selectList();

            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", qingxuList);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("获取情绪数据失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }
}